#include "drv_at24cxx.h"
#include "iic.h"

void drv_at24cxx_wdata(uint8_t addr,uint8_t data)
{
	iicStart();
	drv_iic_sendbyte(0xa0);
	if(drv_iic_waitACK()!=ACK)
	{
		drv_iic_stop();
		return;
	}
	
	drv_iic_sendbyte(addr);
	if(drv_iic_waitACK() != ACK)
	{
		drv_iic_stop();
		return;
	}
	drv_iic_sendbyte(data);
	if(drv_iic_waitACK() != ACK)
	{
		drv_iic_stop();
		return;
	}
	drv_iic_stop();
}

uint8_t drv_at24cxx_rdata(uint8_t addr)
{
	uint8_t read_data = 0;
	drv_iic_start();
	drv_iic_sendbyte(0xa0);
	if(drv_iic_waitACK() != ACK)
	{
		drv_iic_stop();
		return 0;
	}
	drv_iic_sendbyte(addr);
	if(drv_iic_waitACK() != ACK)
	{
		drv_iic_stop();
		return 0;
	}

	drv_iic_start();
	drv_iic_sendbyte(0xa1);
	if(drv_iic_waitACK() != ACK)
	{
		drv_iic_stop();
		return 0;
	}
	read_data = drv_iic_readbyte(NOACK);
	drv_iic_stop();

	return read_data;
}
